﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToWeb.CApExportToWeb
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands.ExportToWeb;

public class CApExportToWeb : IUiExportToWebDataProvider
{
  private string m_destination;
  private string m_dataSetName;
  private int m_LOD;
  private CApExportToWebManager m_manager;
  private string m_operation;
  private bool m_launchBrowser;
  private CApExportToWebViewModel m_ApExportToWebViewModel;

  public CApExportToWeb(CApExportToWebViewModel _apVM)
  {
    this.m_ApExportToWebViewModel = _apVM;
    if (_apVM.Operation != null)
      this.m_operation = ((ITxObject) _apVM.Operation).Name;
    this.m_destination = _apVM.OutputFileName;
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      this.m_dataSetName = _apVM.OutputNameInTC;
      bool? fileCheckedState = _apVM.SaveToFileCheckedState;
      bool flag = false;
      if (fileCheckedState.GetValueOrDefault() == flag & fileCheckedState.HasValue)
      {
        this.m_destination = CApExportToWebUtilities.GetSafeFilename(_apVM.OutputNameInTC);
        this.m_destination = Path.Combine(Path.GetTempPath(), this.m_destination + ".html");
      }
    }
    this.m_launchBrowser = _apVM.LoadBrowser;
    this.m_LOD = _apVM.SelectedLOD;
  }

  public bool Launch()
  {
    this.m_manager = new CApExportToWebManager((IUiExportToWebDataProvider) this);
    this.m_manager.DocumentationSettings.LevelOfDetails = this.m_LOD;
    CApExportToWebManager manager = this.m_manager;
    bool? simulationCheckedState = this.m_ApExportToWebViewModel.ExportSimulationCheckedState;
    bool flag1 = true;
    int num1 = simulationCheckedState.GetValueOrDefault() == flag1 & simulationCheckedState.HasValue ? 1 : 0;
    manager.ExportSimulation = num1 != 0;
    if (CApExportToWebUtilities.IsGraphicViewerEmpty())
    {
      int num2 = (int) TxMessageBox.Show(CApExportToWebUtilities.GetResourceManager().GetString("ERROR_EMPTY_GRAPHIC_VIEWER"), CApExportToWebUtilities.GetResourceManager().GetString("CMD_NAME"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
      return false;
    }
    Cursor.Current = Cursors.WaitCursor;
    this.m_manager.Create();
    bool flag2 = false;
    if (this.m_manager.AnimationDataFileName != null)
    {
      FileInfo fileInfo = new FileInfo(this.m_manager.AnimationDataFileName);
      flag2 = fileInfo.Exists && fileInfo.Length > 0L;
    }
    if (!flag2)
    {
      Cursor.Current = Cursors.Default;
      int num3 = (int) TxMessageBox.Show(CApExportToWebUtilities.GetResourceManager().GetString("ERROR_EXPORT_FAILED"), CApExportToWebUtilities.GetResourceManager().GetString("CMD_NAME"), MessageBoxButtons.OK, MessageBoxIcon.Hand);
      if (this.m_manager.AnimationFramesFileName != null)
      {
        FileInfo fileInfo = new FileInfo(this.m_manager.AnimationFramesFileName);
        try
        {
          fileInfo.Delete();
        }
        catch (Exception ex)
        {
          throw;
        }
      }
      return false;
    }
    CApExportToWebHtmlWriter exportToWebHtmlWriter = new CApExportToWebHtmlWriter(this.m_destination, this.m_operation);
    exportToWebHtmlWriter.AnimationDataFileName = this.m_manager.AnimationDataFileName;
    exportToWebHtmlWriter.AnimationFramesFileName = this.m_manager.AnimationFramesFileName;
    if (this.m_ApExportToWebViewModel.m_isOnAutoTest)
      exportToWebHtmlWriter.m_isOnAutoTest = true;
    exportToWebHtmlWriter.CreateHtmlFile();
    bool? nullable;
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      nullable = this.m_ApExportToWebViewModel.SaveToServerCheckBox;
      bool flag3 = true;
      if (nullable.GetValueOrDefault() == flag3 & nullable.HasValue)
        CApExportToWebUtilities.AttachHTMLFileToProcessInTC(this.m_destination, this.m_dataSetName, this.m_manager.DataProvider.ApExportToWebViewModel.OperationToAttach);
    }
    TxApplication.RefreshDisplay();
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      nullable = this.m_ApExportToWebViewModel.SaveToFileCheckedState;
      bool flag4 = true;
      if (nullable.GetValueOrDefault() == flag4 & nullable.HasValue)
      {
        if (this.m_launchBrowser)
          Process.Start(this.m_destination);
      }
      else if (File.Exists(this.m_destination))
        File.Delete(this.m_destination);
    }
    else if (this.m_launchBrowser)
      Process.Start(this.m_destination);
    Cursor.Current = Cursors.Default;
    return true;
  }

  public string ExportToWebData_Operation => this.m_operation;

  public string ExportToWebData_DestinationHTML => this.m_destination;

  public CApExportToWebViewModel ApExportToWebViewModel => this.m_ApExportToWebViewModel;
}
